using MediatR;
using RBAC.ErrorCode;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace RBAC.Write.Api.Application.Command.ERP.SalesManagement.H
{
    /// <summary>
    /// 编辑（修改）发货通知单命令
    /// </summary>
    public class UpdateDeliveryNoticeCommand : IRequest<APIResult<bool>>
    {
        /// <summary>
        /// 发货通知单主键
        /// </summary>
        [Required]
        public long Id { get; set; }

        /// <summary>
        /// 业务类型（如 普通发货、直运发货）
        /// </summary>
        [Required(ErrorMessage = "业务类型不能为空")]
        public string BizType { get; set; } = string.Empty;

        /// <summary>
        /// 关联销售订单号
        /// </summary>
        public string SalesOrderCode { get; set; } = string.Empty;

        /// <summary>
        /// 客户ID
        /// </summary>
        [Required(ErrorMessage = "客户ID不能为空")]
        public long CustomerId { get; set; }

        /// <summary>
        /// 销售员ID
        /// </summary>
        [Required(ErrorMessage = "销售员ID不能为空")]
        public long SalesmanId { get; set; }

        /// <summary>
        /// 仓库ID
        /// </summary>
        [Required(ErrorMessage = "仓库ID不能为空")]
        public long WarehouseId { get; set; }

        /// <summary>
        /// 收货人
        /// </summary>
        public string Receiver { get; set; } = string.Empty;

        /// <summary>
        /// 收货人电话
        /// </summary>
        public string Phone { get; set; } = string.Empty;

        /// <summary>
        /// 收货人地址
        /// </summary>
        public string Address { get; set; } = string.Empty;

        /// <summary>
        /// 交付日期
        /// </summary>
        public DateTime? DeliveryDate { get; set; }

        /// <summary>
        /// 备注信息
        /// </summary>
        public string Remark { get; set; } = string.Empty;

        /// <summary>
        /// 运费
        /// </summary>
        public decimal? Freight { get; set; }

        /// <summary>
        /// 装卸费
        /// </summary>
        public decimal? LoadingFee { get; set; }

        /// <summary>
        /// 发货明细列表（编辑时整体替换：先逻辑删除旧明细，再插入新明细）
        /// </summary>
        [Required]
        [MinLength(1, ErrorMessage = "至少需要一条发货明细")]
        public List<UpdateDeliveryNoticeItemCommand> Items { get; set; } = new();
    }

    /// <summary>
    /// 编辑发货通知单明细命令（简化为新增整表写入，不做逐条对比）
    /// </summary>
    public class UpdateDeliveryNoticeItemCommand
    {
        [Required(ErrorMessage = "物料ID不能为空")]
        public long MaterialId { get; set; }

        [Required(ErrorMessage = "物料编码不能为空")]
        public string MaterialCode { get; set; }

        [Required(ErrorMessage = "物料名称不能为空")]
        public string MaterialName { get; set; }

        [Required(ErrorMessage = "主计量单位不能为空")]
        public string Unit { get; set; }

        [Required(ErrorMessage = "发货数量不能为空")]
        [Range(0.01, double.MaxValue, ErrorMessage = "发货数量必须大于0")]
        public decimal Qty { get; set; }

        public int PackageQty { get; set; }

        public string BatchNo { get; set; } = string.Empty;

        public string Remark { get; set; } = string.Empty;
    }
}
